import  requests ,json,csv,datetime,os
import  pandas as pd
from  lxml import etree
from pyecharts.charts import  Map,Page,Bar,Timeline
from  pyecharts import  options as  opts
from pyecharts.globals import  ThemeType
headers={"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.50 Safari/537.36"}
r=requests.get("http://pm25.in/rank",headers=headers).text
text=etree.HTML(r)
test=text.xpath("/html/body/div[2]/div/div[7]/table/tbody/tr/td[2]/a/text()")
a=[]
for i in range(len(test)):
    test1=text.xpath("/html/body/div[2]/div/div[7]/table/tbody/tr[{}]/td/text()".format(i+1))
    test1.insert(1,test[i])
    print(test1)
    if test1[4]=="颗粒物(PM2.5)":
        pass
    elif test1[4]=="颗粒物(PM10)":
        pass
    elif test1[4]=="\n                      颗粒物(PM2.5)\n                      "or test1[4]=='\n                      臭氧8小时\n                      ':
        del  test1[4]
        del test1[4]
        del  test1[4]
        test1.insert(4, "颗粒物(PM2.5) and 颗粒物(PM10)")
    elif test1[4]=='臭氧8小时' or test1[4]=='臭氧1小时':
        pass
    elif test1[4]=='\n                      臭氧8小时\n                      ':
        del  test1[4]
        del  test1[4]
        test1.insert(4,"pm10 and 臭氧")
    else:
        test1.insert(4,"无任何污染")
    a.append(test1)
def save_data():
    with open('./csv/{}.csv'.format(datetime.date.today()),"w",encoding="utf-8") as csv_f :
        writer = csv.writer(csv_f, dialect='excel')
        writer.writerow(["排名","城市","AQI","空气质量指数类别","	首要污染物","	PM 2.5细颗粒物","	PM 10可吸入颗粒物","	CO一氧化碳","	NO2二氧化氮","O3臭氧1小时平均	","O3_8h臭氧8小时平均","	SO2二氧化硫"])
        for j in a:
            writer.writerow(j) #保存

def get_file_name(path_str):
    file_name_list = []
    for root, dirs, files in os.walk(path_str):
        for file in files:
            file_name_list.append(os.path.join(file)[:10])
    return file_name_list

def draw_of_picture_of_china():
    tl = Timeline()
    filename = get_file_name('./csv')
    for i in filename:
        data = pd.read_csv('./csv/{}.csv'.format(i))
        name1 = list(data['城市'][:20])
        pm25 = list(data["	PM 2.5细颗粒物"][:20])
        name=list(data['城市'][:20])
        newpm25=list(data["	PM 2.5细颗粒物"][-20:])
        bar = (Bar()
               .add_xaxis(name1)
               .add_yaxis('pm2.5', pm25)
               .set_series_opts(label_opts=opts.LabelOpts(is_show=False),
                                markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="55"), ]))
               .set_global_opts(title_opts=opts.TitleOpts(title="pm2.5（前）"))
               )
        tl.add(bar,'{}'.format(i))
    return tl
def data_view():
    tl = Timeline()
    filename = get_file_name('./csv')
    for i in filename:
        data = pd.read_csv('./csv/{}.csv'.format(i))
        name1 = list(data['城市'][:20])
        pm25 = list(data["	PM 2.5细颗粒物"][:20])
        name=list(data['城市'][-20:])
        newpm25=list(data["	PM 2.5细颗粒物"][-20:])
        bar = (Bar()
               .add_xaxis(name)
               .add_yaxis('pm2.5', newpm25)
               .set_series_opts(label_opts=opts.LabelOpts(is_show=False),
                                markpoint_opts=opts.MarkPointOpts(data=[opts.MarkPointItem(type_="max", name="55"), ]))
               .set_global_opts(title_opts=opts.TitleOpts(title="pm2.5(后)"))
               )
        tl.add(bar,'{}'.format(i))
    return tl
# def nenv_data():
#     for i in range(len(test)):

if __name__=="__main__":
    save_data()
    china=draw_of_picture_of_china()
    pige=Page()
    pige.add(china)
    chiba=data_view()
    pige.add(chiba)
    pige.render('aaa.html')